package com.demo.controller;

import com.demo.dao.UserRepository;
import com.demo.pojo.User;
import com.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/16 21:20
 * @Version 1.0
 **/
@Api("用户登陆与注册")
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;



    @RequestMapping("login")
    @ResponseBody
    @ApiOperation(value = "login",notes = "登陆信息验证")
    public String login(@RequestBody User user) {
        System.out.println(user);
        boolean login = userService.login(user);
        if (login) {
            return "success";
        }
        return "fail";
    }

    @RequestMapping("sender_code")
    @ResponseBody
    public String sender_code(@RequestBody User user, HttpSession session){
        return userService.sender_code(user,session);
    }

    @RequestMapping("register")
    @ResponseBody
    public String register(@RequestBody User user, HttpSession session){
        return userService.register(user,session);
    }

    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
}
